@@ -0,0 +1,30 @@ |
||
| 1 |
+module TwitterConcern |
|
| 2 |
+ extend ActiveSupport::Concern |
|
| 3 |
+ |
|
| 4 |
+ included do |
|
| 5 |
+ self.validate :validate_twitter_options |
|
| 6 |
+ self.after_initialize :configure_twitter |
|
| 7 |
+ end |
|
| 8 |
+ |
|
| 9 |
+ def validate_twitter_options |
|
| 10 |
+ unless options[:consumer_key].present? && |
|
| 11 |
+ options[:consumer_secret].present? && |
|
| 12 |
+ options[:oauth_token].present? && |
|
| 13 |
+ options[:oauth_token_secret].present? |
|
| 14 |
+ errors.add(:base, "consumer_key, consumer_secret, oauth_token and oauth_token_secret are required to authenticate with the Twitter API") |
|
| 15 |
+ end |
|
| 16 |
+ end |
|
| 17 |
+ |
|
| 18 |
+ def configure_twitter |
|
| 19 |
+ Twitter.configure do |config| |
|
| 20 |
+ config.consumer_key = options[:consumer_key] |
|
| 21 |
+ config.consumer_secret = options[:consumer_secret] |
|
| 22 |
+ config.oauth_token = options[:oauth_token] |
|
| 23 |
+ config.oauth_token_secret = options[:oauth_token_secret] |
|
| 24 |
+ end |
|
| 25 |
+ end |
|
| 26 |
+ |
|
| 27 |
+ module ClassMethods |
|
| 28 |
+ |
|
| 29 |
+ end |
|
| 30 |
+end |
@@ -2,6 +2,7 @@ require "twitter" |
||
| 2 | 2 |
|
| 3 | 3 |
module Agents |
| 4 | 4 |
class TwitterPublishAgent < Agent |
| 5 |
+ include TwitterConcern |
|
| 5 | 6 |
cannot_be_scheduled! |
| 6 | 7 |
|
| 7 | 8 |
description <<-MD |
@@ -19,13 +20,9 @@ module Agents |
||
| 19 | 20 |
|
| 20 | 21 |
def validate_options |
| 21 | 22 |
unless options[:username].present? && |
| 22 |
- options[:expected_update_period_in_days].present? && |
|
| 23 |
- options[:consumer_key].present? && |
|
| 24 |
- options[:consumer_secret].present? && |
|
| 25 |
- options[:oauth_token].present? && |
|
| 26 |
- options[:oauth_token_secret].present? |
|
| 27 |
- errors.add(:base, "expected_update_period_in_days, username, consumer_key, consumer_secret, oauth_token and oauth_token_secret are required") |
|
| 28 |
- end |
|
| 23 |
+ options[:expected_update_period_in_days].present? |
|
| 24 |
+ errors.add(:base, "username and expected_update_period_in_days are required") |
|
| 25 |
+ end |
|
| 29 | 26 |
end |
| 30 | 27 |
|
| 31 | 28 |
def working? |
@@ -72,13 +69,6 @@ module Agents |
||
| 72 | 69 |
end |
| 73 | 70 |
|
| 74 | 71 |
def publish_tweet text |
| 75 |
- Twitter.configure do |config| |
|
| 76 |
- config.consumer_key = options[:consumer_key] |
|
| 77 |
- config.consumer_secret = options[:consumer_secret] |
|
| 78 |
- config.oauth_token = options[:oauth_token] |
|
| 79 |
- config.oauth_token_secret = options[:oauth_token_secret] |
|
| 80 |
- end |
|
| 81 |
- |
|
| 82 | 72 |
Twitter.update(text) |
| 83 | 73 |
end |
| 84 | 74 |
|
@@ -2,6 +2,8 @@ require "twitter" |
||
| 2 | 2 |
|
| 3 | 3 |
module Agents |
| 4 | 4 |
class TwitterUserAgent < Agent |
| 5 |
+ include TwitterConcern |
|
| 6 |
+ |
|
| 5 | 7 |
cannot_receive_events! |
| 6 | 8 |
|
| 7 | 9 |
description <<-MD |
@@ -39,9 +41,10 @@ module Agents |
||
| 39 | 41 |
default_schedule "every_1h" |
| 40 | 42 |
|
| 41 | 43 |
def validate_options |
| 42 |
- unless options[:username].present? && options[:expected_update_period_in_days].present? && options[:consumer_key].present? && options[:consumer_secret].present? && options[:oauth_token].present? && options[:oauth_token_secret].present? |
|
| 43 |
- errors.add(:base, "expected_update_period_in_days, username, consumer_key, consumer_secret, oauth_token and oauth_token_secret are required") |
|
| 44 |
- end |
|
| 44 |
+ unless options[:username].present? && |
|
| 45 |
+ options[:expected_update_period_in_days].present? |
|
| 46 |
+ errors.add(:base, "username and expected_update_period_in_days are required") |
|
| 47 |
+ end |
|
| 45 | 48 |
end |
| 46 | 49 |
|
| 47 | 50 |
def working? |
@@ -60,13 +63,6 @@ module Agents |
||
| 60 | 63 |
end |
| 61 | 64 |
|
| 62 | 65 |
def check |
| 63 |
- Twitter.configure do |config| |
|
| 64 |
- config.consumer_key = options[:consumer_key] |
|
| 65 |
- config.consumer_secret = options[:consumer_secret] |
|
| 66 |
- config.oauth_token = options[:oauth_token] |
|
| 67 |
- config.oauth_token_secret = options[:oauth_token_secret] |
|
| 68 |
- end |
|
| 69 |
- |
|
| 70 | 66 |
since_id = memory[:since_id] || nil |
| 71 | 67 |
opts = {:count => 200, :include_rts => true, :exclude_replies => false, :include_entities => true, :contributor_details => true}
|
| 72 | 68 |
opts.merge! :since_id => since_id unless since_id.nil? |